package org.dromara.demo.designPattern.责任链模式.service.impl;

import org.dromara.demo.designPattern.责任链模式.controller.AuthLink;
import org.dromara.demo.designPattern.责任链模式.entity.AuthInfo;
import org.dromara.demo.designPattern.责任链模式.service.AuthService;

import java.text.ParseException;
import java.util.Date;

public class Level2AuthLink extends AuthLink {
    private Date beginDate = f.parse("2020-06-01 00:00:00");
    private Date endDate = f.parse("2020-06-25 23:59:59");
    public Level2AuthLink(String levelUserId, String levelUserName) throws ParseException {
        super(levelUserId, levelUserName);
    }

    public AuthInfo doAuth(String uId, String orderId, Date authDate) {
        // 查询二级审批负责人审批时间
        Date date = AuthService.queryAuthInfo(levelUserId, orderId);
        // 如果没有审批时间，二级审批人没有审批过，则直接返回二级审批负责人待审批状态
        if (null == date) {
            return new AuthInfo("0001", "单号：", orderId, " 状态：待二级审批负责人 ", levelUserName);
        }
        // 如果有审批时间，二级审批人已经审批过，则判断是否有下个审批人，如果有则继续向下传递，如果没有则返回二级审批完成状态
        AuthLink next = super.next();
        if (null == next) {
            return new AuthInfo("0000", "单号：", orderId, " 状态：二级审批完成负责人", " 时间：", f.format(date), " 审批人：",
                    levelUserName);
        }
        //判断时间是否在三级审批时间内
        if (authDate.before(beginDate) || authDate.after(endDate)) {
            return new AuthInfo("0000", "单号：", orderId, " 状态：二级审批完成负责人", " 时间：", f.format(date), " 审批人：",
                    levelUserName);
        }
        return next.doAuth(uId, orderId, authDate);
    }

}
